﻿Imports System.Globalization
Imports System.IO

Partial Public Class Billing_Today
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "BILLING REPORT "
            txtDate.Text = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))
        End If
    End Sub
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If (txtDate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Date !!');", True)
            Exit Sub
        End If
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim dt As DataTable = clsGet.GetSaleOrder_Billing_Today(txtDate.Text.Split(" ")(2) & CDate(txtDate.Text).ToString("MMdd"))
        For Each dr As DataRow In dt.Rows
            Dim Tax As String = ""
            Dim dSM As DataTable = clsGet.Get_SaleORDER_Material(dr.Item("Iport_no"))
            For Each da As DataRow In dSM.Rows
                If (da.Item("Material") & "" = "WP") Then
                    Tax = "/"
                End If
            Next
            dr.Item("Tax") = Tax
            dt.AcceptChanges()
        Next
        If (dt.Rows.Count > 0) Then
            dsReport.Tables.Add(dt.Copy)
            dsReport.AcceptChanges()
            ReportName = "billing.rdlc"
            reportDataSource = "Billing_Value"
            paraName = "Billing_Date"
            paraValue = CDate(txtDate.Text).ToString("dd MMMM ") & txtDate.Text.Split(" ")(2) _
                & "," & DateTime.Now.ToString()
            Orientation = True
            reportType = radioType.SelectedValue
            'Dim path As String = Server.MapPath("~/Uploads/")

            'reportType = "excel"

            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")
            Dim oldFile As String = String.Empty
            oldFile = Server.MapPath(urlDisplay)
            Dim fileInfo As FileInfo = New FileInfo(oldFile)
            Dim reportPath As String = "../WriteReport/"

            Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
            newFileName = "Billing_Report" & newFileName

            If fileInfo.Exists Then
                fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
                urlDisplay = reportPath & newFileName & fileInfo.Extension
            End If


            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบรายการ Billing');", True)
        End If
    End Sub


End Class